from importlib import resources
import os

# 支持中英文提示词
def load_prompt_understanding(language='en'):
    """
    加载提示词文件

    Args:
        language: 'en' for English, 'cn' for Chinese
    """
    if language == 'cn':
        filename = 'PROMPT_UNDERSTANDING_CN.md'
    else:
        filename = 'PROMPT_UNDERSTANDING.md'

    try:
        with (
            resources.files('incloud_mcp_server.static')
            .joinpath(filename)
            .open('r', encoding='utf-8') as f
        ):
            content = f.read()
    except FileNotFoundError:
        # 如果中文文件不存在，回退到英文
        with (
            resources.files('incloud_mcp_server.static')
            .joinpath('PROMPT_UNDERSTANDING.md')
            .open('r', encoding='utf-8') as f
        ):
            content = f.read()

    # 处理术语提醒占位符
    if '{{TERMINOLOGY_REMINDER}}' in content:
        try:
            from ..terminology import get_terminology_reminder
            terminology_reminder = get_terminology_reminder(language)
            content = content.replace('{{TERMINOLOGY_REMINDER}}', terminology_reminder)
        except ImportError:
            # 如果无法导入术语模块，移除占位符
            content = content.replace('{{TERMINOLOGY_REMINDER}}', '')

    return content

# 默认加载英文版本，可以通过环境变量控制
language = os.getenv('INCLOUD_MCP_LANGUAGE', 'en')
PROMPT_UNDERSTANDING = load_prompt_understanding(language)
